package com.yuncheng.oaApi.controller;

import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.entity.NkRsXmbxmbdl;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.service.INkRsXmbxmbdlService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.NkRsXmbxmbxl;
import com.yuncheng.oaApi.service.INkRsXmbxmbxlService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 内控-项目报销模板小类
 * @Author: lhb
 * @Date: 2023-11-24
 * @Version: V1.0
 */
@Api(tags = "NK_内控-项目报销模板小类接口")
@RestController
@RequestMapping("/api/spzx-oa/nkRsXmbxmbxl")
public class NkRsXmbxmbxlController extends BaseController<NkRsXmbxmbxl, INkRsXmbxmbxlService> {

    private static final Logger log = LoggerFactory.getLogger(NkRsXmbxmbxlController.class);

    @Resource
    private INkRsXmbxmbxlService nkRsXmbxmbxlService;

    @Resource
    @Lazy
    private INkRsXmbxmbdlService nkRsXmbxmbdlService;


    /**
     * 分页列表查询
     *
     * @param nkRsXmbxmbxl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "内控-项目报销模板小类-分页列表查询")
    @ApiOperation(value = "内控-项目报销模板小类-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(NkRsXmbxmbxl nkRsXmbxmbxl, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<NkRsXmbxmbxl> queryWrapper = QueryBuilder.initQueryWrapper(nkRsXmbxmbxl, req.getParameterMap());
        Page<NkRsXmbxmbxl> page = new Page<>(pageNo, pageSize);
        IPage<NkRsXmbxmbxl> pageList = nkRsXmbxmbxlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    /**
     * 添加
     *
     * @param nkRsXmbxmbxl
     * @return
     */
    @AutoLog(value = "内控-项目报销模板小类-添加")
    @ApiOperation(value = "内控-项目报销模板小类-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkRsXmbxmbxl nkRsXmbxmbxl) {
        HttpResult<String> result = new HttpResult(true);
        if (StringUtils.isBlank(nkRsXmbxmbxl.getFxmbxmbdlid())){
            throw new BusinessException("所属大类id为空，请重新选择添加");
        }

        //更新小类表字段的大类表id 查询大类表是否有此数据，如果没有直接抛出错误
        List<NkRsXmbxmbdl> nkrsdlList = nkRsXmbxmbdlService.list(new LambdaQueryWrapper<NkRsXmbxmbdl>()
                                        .eq(NkRsXmbxmbdl::getId, nkRsXmbxmbxl.getFxmbxmbdlid()));

        if (nkrsdlList.size() == 0){
            throw new BusinessException("没有查询到对应的项目大类，请联系管理员");
        }

        nkRsXmbxmbxlService.save(nkRsXmbxmbxl);
        result.setResult(nkRsXmbxmbxl.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    /**
     * 编辑
     *
     * @param nkRsXmbxmbxl
     * @return
     */
    @AutoLog(value = "内控-项目报销模板小类-编辑")
    @ApiOperation(value = "内控-项目报销模板小类-编辑")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody NkRsXmbxmbxl nkRsXmbxmbxl) {
        nkRsXmbxmbxlService.updateById(nkRsXmbxmbxl);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-项目报销模板小类-通过id删除")
    @ApiOperation(value = "内控-项目报销模板小类-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        nkRsXmbxmbxlService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "内控-项目报销模板小类-批量删除")
    @ApiOperation(value = "内控-项目报销模板小类-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        nkRsXmbxmbxlService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-项目报销模板小类-通过id查询")
    @ApiOperation(value = "内控-项目报销模板小类-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        NkRsXmbxmbxl nkRsXmbxmbxl = nkRsXmbxmbxlService.getById(id);
        return HttpResult.ok(nkRsXmbxmbxl);
    }

}
